﻿using Newtonsoft.Json;
using System.Data;

namespace OnWork.DataCenter.BLL.Standard.Conver
{
    /// <summary>
    /// 郭家湾转换类
    /// </summary>
    internal class JsonToDataTableGJWConver : IJsonToDataTableConver
    {
        /// <summary>
        /// 转换方法
        /// </summary>
        /// <param name="jsonStr"></param>
        /// <param name="config"></param>
        /// <returns></returns>
        public DataTable Conver(string jsonStr, JsonToDataTableConfig config)
        {
            List<GJWModel> results = JsonConvert.DeserializeObject<List<GJWModel>>(jsonStr);
            if (results != null)
            {
                DataTable dataTable = new DataTable();
                //创建列
                dataTable.Columns.Add(new DataColumn("SensorID", typeof(string))); //传感器ID
                dataTable.Columns.Add(new DataColumn("SensorLocation", typeof(string))); //传感器描述
                dataTable.Columns.Add(new DataColumn("SensorValue", typeof(string))); //传感器的值
                dataTable.Columns.Add(new DataColumn("CreateTime", typeof(DateTime))); //创建时间
                dataTable.Columns.Add(new DataColumn("DataTime", typeof(DateTime))); //数据时间

                foreach (var item in results)
                {
                    //创建行
                    DataRow dr = dataTable.NewRow();

                    dr[0] = item.id;
                    dr[1] = item.value.tagdesp;

                    string? val = null;
                    if (item.value.dataType == "double")
                    {
                        val = item.value.doubleValue?.ToString();
                    }
                    else if (item.value.dataType == "single")
                    {
                        val = item.value.singleValue?.ToString();
                    }
                    else if (item.value.dataType == "message")
                    {
                        val = item.value.message;
                    }
                    dr[2] = val;
                    dr[3] = DateTime.Now;
                    dr[4] = TimeStampToDate(item.value.time);

                    dataTable.Rows.Add(dr);
                }

                return dataTable;
            }

            return null;
        }

        /// <summary>
        /// 时间戳转日期
        /// </summary>
        /// <param name="timestamp"></param>
        /// <returns></returns>
        private DateTime TimeStampToDate(long timestamp)
        {
            try
            {
                long begtime = timestamp * 10000;
                DateTime dt_1970 = new DateTime(1970, 1, 1, 0, 0, 0);
                long tricks_1970 = dt_1970.Ticks;//1970年1月1日刻度
                long time_tricks = tricks_1970 + begtime;//日志日期刻度

                if (time_tricks > DateTime.MinValue.Ticks && time_tricks < DateTime.MaxValue.Ticks)
                {
                    DateTime dt = new DateTime(time_tricks);//转化为DateTime
                    dt = dt.AddHours(8);//转换成北京时间
                    return dt;
                }
                else
                {
                    return DateTime.Now;
                }
            }
            catch (Exception ex)
            {
                string msg = ex.Message;
                throw;
            }
        }
    }

    public class GJWModelValue
    {
        /// <summary>
        ///数据值类型(包括double/single/message)
        /// </summary>
        public string dataType { get; set; }

        /// <summary>
        ///单精度
        /// </summary>
        public float? singleValue { get; set; }

        /// <summary>
        ///双精度
        /// </summary>
        public double? doubleValue { get; set; }

        /// <summary>
        ///测点时间 时间戳
        /// </summary>
        public long time { get; set; }

        /// <summary>
        /// 整形或者布尔型
        /// </summary>
        public string state { get; set; }

        /// <summary>
        /// 数据质量
        /// </summary>
        public int flags { get; set; }

        /// <summary>
        /// 文本类型 数据
        /// </summary>
        public string message { get; set; }

        /// <summary>
        /// 单位
        /// </summary>
        public string munit { get; set; }

        /// <summary>
        /// 51202主回撤通道皮带机尾烟雾
        /// </summary>
        public string tagdesp { get; set; }
    }

    /// <summary>
    /// 返回模型类
    /// </summary>
    public class GJWModel
    {
        /// <summary>
        /// 测点编码
        /// </summary>
        public string id { get; set; }

        /// <summary>
        /// 数据湖id
        /// </summary>
        public string datalakeId { get; set; }

        /// <summary>
        ///组织机构编码
        /// </summary>
        public string org { get; set; }

        /// <summary>
        ///数据来源
        /// </summary>
        public string ioaddress { get; set; }

        /// <summary>
        ///数据类型
        /// </summary>
        public string dataType { get; set; }

        /// <summary>
        ///数据值
        /// </summary>
        public GJWModelValue value { get; set; }
    }
}